//
//  HSLotteryNumberDetailVC.m
//  HolyShit
//
//  Created by Robin on 2017/4/30.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSLotteryNumberDetailVC.h"
#import "HSLotteryNumberDetailCell.h"

@interface HSLotteryNumberDetailVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<HSLotteryNumberDetailModel *> *models;
@property (nonatomic, copy) NSString *lotId;
@property (nonatomic, assign) NSUInteger pageIndex;

@end

@implementation HSLotteryNumberDetailVC

- (instancetype)initWithLotId:(NSString *)lotId lotName:(NSString *)name
{
    if (self = [super init]) {
        _lotId = lotId;
        self.title = [NSString stringWithFormat:@"%@ 开奖号码", name];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableView];
    @weakify(self);
    [self.hintView setReloadBlock:^{
        @strongify(self);
        self.hintView.hintState = MYHintStateLoading;
        [self refresh];
    }];
    self.models = @[].mutableCopy;
    self.hintView.reloadBlock();
}

- (void)refresh
{
    NSDictionary *params = @{@"opencode_detail": @{@"page_index": @0,
                                                   @"lotType": self.lotId,
                                                   @"page_size": @"10"},
                             @"c_head": @{@"client_id": @"BY003000000000000002",
                                          @"client_os": @"IOS"}};
    @weakify(self);
    [[MYHttpHelper shareHelper] postWithUrl:@"http://mycp.iplay78.com/trade-web/web/opencode_detail" params:params success:^(NSDictionary *dict) {
        @strongify(self);
        NSDictionary *opencode_detail = dict[@"opencode_detail"];
        NSDictionary *detail = opencode_detail[@"detail"];
        self.models = [NSArray modelArrayWithClass:[HSLotteryNumberDetailModel class] json:detail[@"detail_list"]].mutableCopy;
        if (self.models.count > 0 && self.models[0].canExpend) {
            self.models[0].isExpend = YES;
        }
        self.pageIndex = 0;
        self.hintView.hintState = MYHintStateHidden;
        [self.tableView endRefreshAndLoadMore];
        [self.tableView reloadData];
    } failed:^(NSError *error) {
        @strongify(self);
        self.hintView.hintState = MYHintStateNoData;
        [self.tableView endRefreshAndLoadMore];
        [UIWindow showError:error];
    }];
}

- (void)loadmore
{
    NSDictionary *params = @{@"opencode_detail": @{@"page_index": @(self.pageIndex + 10),
                                                   @"lotType": self.lotId,
                                                   @"page_size": @"10"},
                             @"c_head": @{@"client_id": @"BY003000000000000002",
                                          @"client_os": @"IOS"}};
    @weakify(self);
    [[MYHttpHelper shareHelper] postWithUrl:@"http://mycp.iplay78.com/trade-web/web/opencode_detail" params:params success:^(NSDictionary *dict) {
        @strongify(self);
        NSDictionary *opencode_detail = dict[@"opencode_detail"];
        NSDictionary *detail = opencode_detail[@"detail"];
        NSArray *array = [NSArray modelArrayWithClass:[HSLotteryNumberDetailModel class] json:detail[@"detail_list"]];
        if (array.count > 0) {
            [self.models addObjectsFromArray:array];
            self.pageIndex += 10;
        }
        self.hintView.hintState = MYHintStateHidden;
        [self.tableView endRefreshAndLoadMore];
        [self.tableView reloadData];
    } failed:^(NSError *error) {
        @strongify(self);
        self.hintView.hintState = MYHintStateNoData;
        [self.tableView endRefreshAndLoadMore];
        [UIWindow showError:error];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.models.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSLotteryNumberDetailModel *model = self.models[indexPath.row];
    return model.isExpend ? model.expendHeight : model.normalHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSLotteryNumberDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HSLotteryNumberDetailCell class])];
    cell.model = self.models[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSLotteryNumberDetailModel *model = self.models[indexPath.row];
    if (model.canExpend) {
        model.isExpend = !model.isExpend;
        [self.tableView reloadRow:indexPath.row inSection:0 withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - NavBarHeight)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView addRefreshTarget:self action:@selector(refresh)];
        [_tableView addLoadMoreTarget:self action:@selector(loadmore)];
        [_tableView registerClass:[HSLotteryNumberDetailCell class] forCellReuseIdentifier:NSStringFromClass([HSLotteryNumberDetailCell class])];
    }
    return _tableView;
}

@end
